import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import legacyPlugin from '@vitejs/plugin-legacy'
import Components from 'unplugin-vue-components/vite'
import { VantResolver } from 'unplugin-vue-components/resolvers'
export default ({ mode }) => {
    return defineConfig({
        plugins: [
            vue(),
            AutoImport({
                include: [/\.[tj]sx?$/, /\.vue$/, /\.vue\?vue/],
                imports: [
                    'vue',
                    'vue-router',
                    {
                        'xe-utils': [['default', 'XEUtils']],
                        lodash: [['default', '_']],
                        axios: [['default', 'axios']],
                        dayjs: [['default', 'dayjs']],
                        mockjs: [['default', 'Mock']],
                        'better-scroll': [['default','BScroll','BetterScroll']],
                        'dingtalk-jsapi': [['default', 'dd']],
                        qs: [['default', 'qs']],
                        vant: [['default', 'Vant'], 'showToast', 'showLoadingToast', 'showSuccessToast', 'showFailToast', 'closeToast'],
                    },
                ],
                dirs: ['./src/javascript/store/**', './src/javascript/tool/**', './src/javascript/antdesign', './src/web/localData/**'],
                dts: 'src/javascript/tool/auto-imports.d.js',
            }),
            legacyPlugin({
                targets: ['chrome 52'],
                additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
            }),
            Components({
                resolvers: [VantResolver()],
            }),
        ],
        base: process.env.NODE_ENV === 'production' ? './' : '/',
        build: {
            chunkSizeWarningLimit: 3000,
        },
        resolve: {
            alias: {
                '@': resolve(__dirname, './src'),
            },
        },
        server: {
            port: 3316,
            host: '0.0.0.0',
            proxy: {
                '/api': {
                    target: 'http://daohe.dingapps.cn',
                    changeOrigin: true,
                    rewrite: path => path.replace(/^\/api/, ''),
                },
            },
        },
    })
}
